#include <QDebug>
#include <QFileDialog>
#include <QKeyEvent>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "GLDisplay.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    colorDialog.setOption(QColorDialog::NoButtons);

    connect(ui->actionWireFrame, SIGNAL(toggled(bool)), this, SIGNAL(wireframeClicked(bool)));
    connect(ui->actionLight_Rotation, SIGNAL(toggled(bool)), this, SIGNAL(lightRotationToggle(bool)));
    connect(ui->actionSaveImageAs, SIGNAL(triggered()), this, SIGNAL(saveResultAsImage()));
    connect(ui->shaderCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(toggleShader(bool)));
    connect(ui->scaleSlider, SIGNAL(valueChanged(int)), this, SIGNAL(scaleFactorChanged(int)));

    connect(ui->colorButton, SIGNAL(clicked()), this, SLOT(openColorDialog()));
    connect(ui->textureButton, SIGNAL(clicked()), this, SLOT(openTextureFileDialog()));
    connect(&colorDialog, SIGNAL(currentColorChanged(QColor)), this, SIGNAL(colorChanged(QColor)));

    //connect(ui->objetosComboBox, SIGNAL(currentIndexChanged(QString)), this, SIGNAL(changeObjectSelected(QString)));
    connect(ui->insertButton, SIGNAL(clicked()), this, SLOT(getObjectChosen()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setGLDisplay(GLDisplay *display)
{
    ui->verticalLayout->addWidget(display);
}

void MainWindow::toggleCheckbox(bool state)
{
    if (state) {
        ui->shaderCheckBox->setCheckState(Qt::Checked);
    } else {
        ui->shaderCheckBox->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::changeScaleSliderValue(int value)
{
    ui->scaleSlider->setValue(value);
}

void MainWindow::openColorDialog()
{
    colorDialog.exec();
}

void MainWindow::openTextureFileDialog()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Selecione a textura"), NULL, tr("Image Files (*.png *.jpg *.bmp)"));
    emit textureChanged(fileName);
}

void MainWindow::getObjectChosen() {
    emit insertObjectCliked(ui->objetosComboBox->currentText());
}

void MainWindow::keyPressEvent(QKeyEvent * key)
{
    GLDisplay::keyboard(key->key());

    if (key->key() == Qt::Key_Delete) {
        emit deletePressed();
    }

    emit updateRender();
}
